package com.recruit.g1.resume.controller;

import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.common.response.StateCode;
import com.recruit.g1.pojo.resume.dto.ResumeCreate;
import com.recruit.g1.pojo.user.vo.UserSaveParam;
import com.recruit.g1.resume.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@Api(tags = "简历模块")
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    ResumeService resumeService;


    @ApiOperation(value = "创建简历")
    @PostMapping("create")
    public JsonResult createResume(@RequestBody ResumeCreate resumeCreate, @ApiIgnore HttpSession session){
        log.debug("进入简历创建：{}" , resumeCreate);
        try{
            return resumeService.create(resumeCreate, session);
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.fail("创建失败");
    }


    @ApiOperation(value = "编辑简历（选择已创建简历）")
    @GetMapping("toEdit")
    public JsonResult getEditResumeById(Long id,@ApiIgnore HttpSession session){
        log.debug("进入简历编辑：");
        try {

           /* if (session.getAttribute("currentLoginUser")==null){
                throw new RuntimeException("没有登陆");
            }*/

            /**
             * 剩余逻辑：
             * 先选择简历，然后查看，
             * 点击编辑，进入编辑。
             * 点击保存（创建/更新）。
             */

            return JsonResult.ok( resumeService.editById(id) );
        }catch (Exception e){
            e.printStackTrace();
        }

        return JsonResult.fail("修改失败");
    }


    @ApiOperation(value = "编辑简历")
    @GetMapping("edit")
    public JsonResult editResumeById(Long id,@ApiIgnore HttpSession session){
        log.debug("进入简历编辑：");
        try {

           /* if (session.getAttribute("currentLoginUser")==null){
                throw new RuntimeException("没有登陆");
            }*/

            /**
             * 剩余逻辑：
             * 先选择简历，然后查看，
             * 点击编辑，进入编辑。
             * 点击保存（创建/更新）。
             */

            /// ///////////////////////////////这里缺少代码

            return JsonResult.ok(  );
        }catch (Exception e){
            e.printStackTrace();
        }

        return JsonResult.fail("修改失败");
    }



















    @ApiOperation(value = "选择简历")
    @GetMapping("select")
    public JsonResult selectResumeById(Long id,@ApiIgnore HttpSession session){
        try {

            /*if (session.getAttribute("currentLoginUser")==null){
                throw new RuntimeException("没有登陆");
            }*/
            return resumeService.selectById(id);

        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.fail("勾选简历失败");
    }

    @ApiOperation(value = "删除简历")
    @PostMapping("delete")
    public JsonResult deleteById(Long id,@ApiIgnore HttpSession session){
        try {

            /*if (session.getAttribute("currentLoginUser")==null){
                return JsonResult.fail(StateCode.NOT_LOGIN);
            }*/

            log.debug("session.getId的结果->"+session.getAttribute(session.getId()));

            return resumeService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @ApiOperation("我的简历")
    @GetMapping("myResumeList")
    public JsonResult myResumeList(@ApiIgnore HttpSession session){
        UserSaveParam userSaveParam = (UserSaveParam) session.getAttribute("currentLoginUser");
        if (userSaveParam==null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }
        /**
         *  userSaveParam.getId();
         */
        return resumeService.selectByUserId(userSaveParam.getId());
    }
}
